package com.by.basicgateway.intercept.filter;

import com.by.toolkit.consts.WebGlobalConst;
import com.by.toolkit.utils.ByIfTrueUtil;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class WxMaAppIdMapProjectInfoGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {

    @Resource
    private GatewayFilterSupport gatewayFilterSupport;

    private static final String WX_MA_URI_PREFIX = "/byWxMa/";

    @Override
    public GatewayFilter apply(Object config) {
        return (exchange, chain) -> {
            ServerHttpRequest request = exchange.getRequest();
            String path = request.getURI().getPath();
            if (!path.startsWith(WX_MA_URI_PREFIX)) {
                return chain.filter(exchange);
            }
            String newPath = path.replace(WX_MA_URI_PREFIX, "/");
            String wxMaAppId = request.getHeaders().getFirst("byWxAppId");
            String headerProjectCode = request.getHeaders().getFirst(WebGlobalConst.PROJECT_CODE);
            if (StringUtils.isBlank(headerProjectCode)) {
                log.info("[WxMaAppIdMapProjectInfoGatewayFilter][filter] path:[{}] headerProjectCodeIsNull", path);
                return chain.filter(exchange);
            }

            ServerWebExchange newExchange = ByIfTrueUtil.getIfTrueOrElse(StringUtils.isNotBlank(wxMaAppId)
                    , () -> {
                        Map<String, String> projectInfoMap = gatewayFilterSupport.projectCodeMapInfo(headerProjectCode);
                        log.info("[WxMaAppIdMapProjectInfoGatewayFilter][filter] path:[{}] newPath:[{}] wxMaAppId:[{}] projectInfoMap:[{}]", path, newPath, wxMaAppId, projectInfoMap);
                        return gatewayFilterSupport.dealHeader(exchange, newPath, projectInfoMap, Collections.emptyList());
                    }
                    , () -> {
                        log.info("[WxMaAppIdMapProjectInfoGatewayFilter][filter] path:[{}] newPath:[{}] wxMaAppIdIsNull", path, newPath);
                        return exchange.mutate()
                                .request(request.mutate().path(newPath).build())
                                .build();
                    }
            );
            return chain.filter(newExchange);
        };
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return List.of("require");
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Config {

        private boolean require;

    }

}
